package com.sojson.controller.po;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.PostParamVerify;
import com.sojson.entity.po.Post;
import com.sojson.entity.vo.PostVo;
import com.sojson.enums.EPostApplyPostsStatus;
import com.sojson.enums.EPostStatus;
import com.sojson.result.Result;
import com.sojson.service.PostService;
import com.sojson.util.DateUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;

/**
 * 职位表控制器
 * 
 * @author liu
 * @date 2021-04-15
 */
@Api(tags = "职位表管理")
@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 单条插入
     * 
     * @param post
     * @return
     * @throws IOException 
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/post/insert')")
    public Object insert(@RequestBody Post post) throws IOException {
        // 校验参数
        Result<?> paramResult = PostParamVerify.insert(post);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        post.insertInit();
        postService.insert(post);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param post
     * @return
     */
    // @PostMapping("/insertList")
    public Object insertList(@RequestBody Post post) {
        return Result.success(postService.insertList(post));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param post
     * @return
     */
    // @DeleteMapping("/deleteByDto")
    public Object deleteByDto(@RequestBody Post post) {
        return Result.success(postService.deleteByDto(post));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    // @GetMapping("/countAll")
    public Object countAll() {
        int countAll = postService.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     */
    // @GetMapping("/findAll")
    public Object findAll() {
        List<Post> findAll = postService.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param post
     * @return
     */
    @GetMapping("/countByDto")
    @PreAuthorize("@ss.hasPermi('/post/countByDto')")
    public Object countByDto(Post post) {
        int countByDto = postService.countByDto(post);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param post
     * @return
     */
    @GetMapping("/findByDto")
    @PreAuthorize("@ss.hasPermi('/post/findByDto')")
    public Result<List<PostVo>> findByDto(Post post) {
        List<PostVo> findByDto = postService.list(post);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    // @DeleteMapping("/deleteById")
    public Object deleteById(Integer id) {
        postService.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     */
    // @DeleteMapping("/deleteByIdList")
    public Object deleteByIdList(@RequestParam("ids") List<Integer> ids) {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(postService.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param post
     * @return
     */
    // @PutMapping("/updateById")
    public Object updateById(@RequestBody Post post) {
        postService.updateById(post);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param post
     * @return
     */
    // @PutMapping("/updateStaticByIdList")
    public Object updateStaticByIdList(@RequestBody Post post) {
        return Result.success(postService.updateStaticByIdList(post));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param post
     * @return
     */
    // @PutMapping("/updateChangeByIdList")
    public Object updateChangeByIdList(@RequestBody Post post) {
        return Result.success(postService.updateChangeByIdList(post));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param post
     * @return
     */
    @PutMapping("/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/post/updatePartialById')")
    public Object updatePartialById(@RequestBody Post post) {
        // 校验参数
        Result<?> paramResult = PostParamVerify.updatePartialById(post);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        post.updateInit();
        int updatePartialById = postService.updatePartialById(post);
        return Result.success(updatePartialById);
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param post
     * @return
     */
    // @PutMapping("/updateStaticPartialByIdList")
    public Object updateStaticPartialByIdList(@RequestBody Post post) {
        return Result.success(postService.updateStaticPartialByIdList(post));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param post
     * @return
     */
    // @PutMapping("/updateChangePartialByIdList")
    public Object updateChangePartialByIdList(@RequestBody Post post) {
        return Result.success(postService.updateChangePartialByIdList(post));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Object findById(Integer id) {
        Post findById = postService.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     */
    // @GetMapping("/findByIdList")
    public Object findByIdList(@RequestParam("ids[]") List<Integer> ids) {
        List<Post> findByIdList = postService.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param post
     * @return
     */
    // @GetMapping("/findIdByDto")
    public Object findIdByDto(Post post) {
        List<Integer> findIdByDto = postService.findIdByDto(post);
        return Result.success(findIdByDto);
    }

    /**
     * 通过userId删除(物理删除)
     * 
     * @param userId
     * @return
     */
    // @DeleteMapping("/deleteByUserId")
    public Object deleteByUserId(Integer userId) {
        postService.deleteByUserId(userId);
        return Result.success();
    }

    /**
     * 通过userId列表删除(物理删除)
     * 
     * @param userIds
     * @return
     */
    // @DeleteMapping("/deleteByUserIdList")
    public Object deleteByUserIdList(@RequestParam("userIds") List<Integer> userIds) {
        return Result.success(postService.deleteByUserIdList(userIds));
    }

    /**
     * 通过userId更新(全部更新)
     * 
     * @param post
     * @return
     */
    // @PutMapping("/updateByUserId")
    public Object updateByUserId(@RequestBody Post post) {
        postService.updateByUserId(post);
        return Result.success();
    }

    /**
     * 通过userId列表更新(全部更新)(更新内容相同)
     * 
     * @param post
     * @return
     */
    // @PutMapping("/updateStaticByUserIdList")
    public Object updateStaticByUserIdList(@RequestBody Post post) {
        return Result.success(postService.updateStaticByUserIdList(post));
    }

    /**
     * 通过userId列表更新(全部更新)(更新内容不相同)
     * 
     * @param posts
     * @return
     */
    // @PutMapping("/updateChangeByUserIdList")
    public Object updateChangeByUserIdList(@RequestBody Post posts) {
        return Result.success(postService.updateChangeByUserIdList(posts));
    }

    /**
     * 通过userId更新(局部更新)
     * 
     * @param post
     * @return
     */
    // @PutMapping("/updatePartialByUserId")
    public Object updatePartialByUserId(@RequestBody Post post) {
        postService.updatePartialByUserId(post);
        return Result.success();
    }

    /**
     * 通过userId列表更新(局部更新)(更新内容相同)
     * 
     * @param post
     * @return
     */
    // @PutMapping("/updateStaticPartialByUserIdList")
    public Object updateStaticPartialByUserIdList(@RequestBody Post post) {
        return Result.success(postService.updateStaticPartialByUserIdList(post));
    }

    /**
     * 通过userId列表更新(局部更新)(更新内容不相同)
     * 
     * @param posts
     * @return
     */
    // @PutMapping("/updateChangePartialByUserIdList")
    public Object updateChangePartialByUserIdList(@RequestBody Post posts) {
        return Result.success(postService.updateChangePartialByUserIdList(posts));
    }

    /**
     * 通过userId查询
     * 
     * @param userId
     * @return
     */
    // @GetMapping("/findByUserId")
    public Object findByUserId(Integer userId) {
        Post findByUserId = postService.findByUserId(userId);
        return Result.success(findByUserId);
    }

    /**
     * 通过userId列表查询
     * 
     * @param userIds
     * @return
     */
    // @GetMapping("/findByUserIdList")
    public Object findByUserIdList(@RequestParam("userIds[]") List<Integer> userIds) {
        List<Post> findByUserIdList = postService.findByUserIdList(userIds);
        return Result.success(findByUserIdList);
    }

    /**
     * 通过对象查询userId列表
     * 
     * @param post
     * @return
     */
    // @GetMapping("/findUserIdByDto")
    public Object findUserIdByDto(Post post) {
        List<Integer> findUserIdByDto = postService.findUserIdByDto(post);
        return Result.success(findUserIdByDto);
    }

    /**
     * 通过对象删除(逻辑删除)
     * 
     * @param post
     * @return
     */
    // @DeleteMapping("/deleteByDtoAndUpdate")
    public Object deleteByDtoAndUpdate(@RequestBody Post post) {
        return Result.success(postService.deleteByDtoAndUpdate(post));
    }

    /**
     * 统计所有未删除的数据
     * 
     * @return
     */
    // @GetMapping("/countAllAndStatusNo")
    public Object countAllAndStatusNo() {
        int countAllAndStatusNo = postService.countAllAndStatusNo();
        return Result.success(countAllAndStatusNo);
    }

    /**
     * 查询所有未删除的数据
     * 
     * @return
     */
    // @GetMapping("/findAllAndStatusNo")
    public Object findAllAndStatusNo() {
        List<Post> findAllAndStatusNo = postService.findAllAndStatusNo();
        return Result.success(findAllAndStatusNo);
    }

    /**
     * 通过id删除(逻辑删除)
     * 
     * @param id
     * @return
     */
    // @DeleteMapping("/deleteByIdAndUpdate")
    public Object deleteByIdAndUpdate(Integer id) {
        postService.deleteByIdAndUpdate(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(逻辑删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIdListAndUpdate")
    @PreAuthorize("@ss.hasPermi('/post/deleteByIdListAndUpdate')")
    public Object deleteByIdListAndUpdate(@RequestParam("ids") List<Integer> ids) {
        if (ids.size() == 0) {
            return Result.success();
        }

        return Result.success(postService.deleteByIdListAndUpdate(ids));
    }

    /**
     * 通过userId删除(逻辑删除)
     * 
     * @param userId
     * @return
     */
    // @DeleteMapping("/deleteByUserIdAndUpdate")
    public Object deleteByUserIdAndUpdate(Integer userId) {
        postService.deleteByUserIdAndUpdate(userId);
        return Result.success();
    }

    /**
     * 通过userId列表删除(逻辑删除)
     * 
     * @param userIds
     * @return
     */
    // @DeleteMapping("/deleteByUserIdListAndUpdate")
    public Object deleteByUserIdListAndUpdate(@RequestParam("userIds") List<Integer> userIds) {
        return Result.success(postService.deleteByUserIdListAndUpdate(userIds));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    // @DeleteMapping("/clear")
    public Object clear() {
        postService.clear();
        return Result.success();
    }

    /**
     * 清空数据(逻辑清空)
     * 
     * @return
     */
    // @DeleteMapping("/clearAndUpdate")
    public Object clearAndUpdate() {
        postService.clearAndUpdate();
        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param id
     * @return
     */
    @PutMapping("/updateStatus")
    @PreAuthorize("@ss.hasPermi('/post/updateStatus')")
    public Object close(@RequestBody Post post) {
        Post ps = new Post();
        ps.updateInit();
        ps.setId(post.getId());
        ps.setStatus(post.getStatus());
        ps.setCloseMsg(post.getCloseMsg());
        return Result.success(postService.updatePartialById(ps));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param id
     * @return
     */
    @PutMapping("/xcxClose/{id}")
    @PreAuthorize("@ss.hasPermi('/post/xcxClose/{id}')")
    public Object xcxClose(@PathVariable Integer id) {
        Post post = new Post();
        post.updateInit();
        post.setId(id);
        post.setStatus(EPostStatus.CLOSE.getCode());
        post.setCloseMsg("自主关闭");
        return Result.success(postService.updatePartialById(post));
    }

    /**
     * 根据对象查询
     * 
     * @param post
     * @return
     */
    @GetMapping("/findLoadingByDto")
    public Object findLoadingByDto(PostVo post) {
        post.setStatus(EPostStatus.LOADING.getCode());
        post.setThisDate(DateUtil.nowDate());
        String expectedSalary = post.getExpectedSalary();
        if (StringUtil.isNotBlank(expectedSalary)) {
            String[] split = expectedSalary.split("-");
            post.setPayStart(new BigDecimal(split[0]).multiply(new BigDecimal("1000")));
            if (split.length > 1) {
                post.setPayEnd(new BigDecimal(split[1]).multiply(new BigDecimal("1000")));
            }
        }
        String orderByColumn = post.getOrderByColumn();
        post.setOrderByColumn("isOutDate, isFull, startYear, startTime"
            + (StringUtil.isBlankString(orderByColumn) ? "" : "," + orderByColumn));
        List<Integer> arrayList = new ArrayList<>();
        arrayList.add(EPostApplyPostsStatus.AUDIT.getCode());
        arrayList.add(EPostApplyPostsStatus.POSTS_YES.getCode());
        post.setPostsStatuss(arrayList);
        List<Post> findByDto = postService.findXcxByDto(post);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param post
     * @return
     * @throws IOException 
     */
    @GetMapping("/findThisByDto")
    @PreAuthorize("@ss.hasPermi('/post/findThisByDto')")
    public Object findThisByDto(Post post) throws IOException {
        post.setUserId(TokenUtil.getUserId());
        List<Post> findByDto = postService.findByDto(post);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象统计数量
     * 
     * @param post
     * @return
     * @throws IOException 
     */
    @GetMapping("/countThisByDto")
    @PreAuthorize("@ss.hasPermi('/post/countThisByDto')")
    public Object countThisByDto(Post post) throws IOException {
        post.setUserId(TokenUtil.getUserId());
        int countByDto = postService.countByDto(post);
        return Result.success(countByDto);
    }

    /**
     * Boss首页统计
     * 
     * @return
     */
    @GetMapping("/countApp")
    @PreAuthorize("@ss.login()")
    public Object countApp() throws Exception {
        PostVo post = new PostVo();
        post.setUserId(TokenUtil.getUserId());

        return Result.success(postService.countApp(post));
    }

}